<template>
  <!-- 空白购物车区域 -->
  <view class="empty-cart" v-if="cart.length === 0">
    <image src="/static/cart_empty@2x.png" class="empty-img"></image>
    <text class="tip-text">空空如也</text>
  </view>
  <view class="cart-container" v-else>
    <!-- 收货地址组件 -->
    <my-address></my-address>
    <!-- 商品列表的标题区域 -->
    <view class="cart-title">
      <!-- 左侧的图标 -->
      <uni-icons type="shop" size="18"></uni-icons>
      <!-- 右侧的文字 -->
      <text class="cart-title-text">购物车</text>
    </view>
    <!-- 循环渲染购物车中的商品信息 -->
    <uni-swipe-action>
          <block v-for="(goods,index) in cart" :key="goods.goods_id">
      <uni-swipe-action-item :right-options="options" @click="swipeItemClickHandler(goods)">
        <my-goods :goods="goods" :showRadio="true" @radio-change="radioChangeHandler" :showNum="true" @num-change="numberChangeHandler">
        </my-goods>
      </uni-swipe-action-item>
          </block>
    </uni-swipe-action> 
    <!-- 使用自定义结算组件 -->
    <my-settle></my-settle>
  </view>
</template>

<script setup>
  import { storeToRefs } from 'pinia';
  import { useCart } from '../../store';
  const cartStore = useCart()
  const {cart} = storeToRefs(cartStore)
  const radioChangeHandler = (e)=>{
    cartStore.updateGoodsState(e)
  }
  const numberChangeHandler = (e)=>{
    cartStore.updateGoodsCount(e)
  }
  const options = [
    {
      text:'删除',
      style:{
        backgroundColor:'#c00000'
      }
    }
  ]
  const swipeItemClickHandler = (goods)=>{
    cartStore.removeGoodsById(goods.goods_id)
  }
  // 封装起来
// import {onShow} from '@dcloudio/uni-app'
// import { storeToRefs } from 'pinia';
// import { useCart } from '../../store';
// const cartStore = useCart()
// const {total} = storeToRefs(cartStore)
// onShow(()=>{
//   setBadge()
// })
// const setBadge = ()=>{
//   uni.setTabBarBadge({
//     index:2,
//     text:total.value + ''
//   })
// }

import { onShow } from '@dcloudio/uni-app';  
import { useTabBarBadge } from '@/hooks/tabbar-badge.js'; 
  
const { onPageShow } = useTabBarBadge();  
  
// onShow(onPageShow); // 在页面显示时调用 onPageShow  
onShow(()=>{
  onPageShow()
})
</script>

<style lang="scss">
  .cart-container{
    padding-bottom: 50px;
  }
.cart-title {
  padding-left: 5px;
  height: 40px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #efefef;
  .cart-title-text{
    margin-left: 10px;
    font-size: 14px;
  }
}
.empty-cart{
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding-top: 150px;
  .empty-img{
    width: 90px;
    height: 90px;
  }
  .tip-text{
    font-size: 12px;
    color: gray;
    margin-top: 15px;
  }
}
</style>
